//
//  GWHandpickTableViewController.m
//  GiftWanted
//
//  Created by lanou on 15/11/6.
//  Copyright © 2015年 陈文杰. All rights reserved.
//

#import "GWHandpickTableViewController.h"
#import "GWItemsCell.h"
#import "GWItemsModel.h"
#import "GWPromotionsModel.h"
#import "GWBannersModel.h"
#import "SDCycleScrollView.h"
#import "SDWebImageManager.h"
#import "SDWebImageDownloader.h"
#import "GWRaidersViewController.h"


@interface GWHandpickTableViewController ()

@property (nonatomic, strong) NSMutableArray *dataSourceArray;
@property (nonatomic, strong) NSMutableArray *bannersArr;
@property (nonatomic, strong) NSMutableArray *promotionsArr;

@property (nonatomic, strong) SDCycleScrollView *cycleScrollView;

@end

@implementation GWHandpickTableViewController
- (NSMutableArray *)dataSourceArray {
    
    if (!_dataSourceArray) {
        _dataSourceArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSourceArray;
}

- (NSMutableArray *)bannersArr {
    
    if (!_bannersArr) {
        _bannersArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _bannersArr;
}

- (NSMutableArray *)promotionsArr {
    
    if (!_promotionsArr) {
        _promotionsArr = [NSMutableArray arrayWithCapacity:0];
    }
    return _promotionsArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerClass:[GWItemsCell class] forCellReuseIdentifier:@"cell"];
    self.tableView.rowHeight = 155;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)configureData {
    
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_urlStr]]];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self.dataSourceArray removeAllObjects];
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
//        NSLog(@"%@", dict);
        for (NSDictionary *modelDic in dict[@"data"][@"items"]) {
            GWItemsModel *model = [[GWItemsModel alloc] init];
            [model setValuesForKeysWithDictionary:modelDic];
            [self.dataSourceArray addObject:model];
        }
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"网络请求失败");
    }];
    [operation start];
    
    AFHTTPRequestOperation *operation1 = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://api.liwushuo.com/v2/banners?channel=iOS"]]];
    [operation1 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        for (NSDictionary *modelDic in dict[@"data"][@"banners"]) {
            GWBannersModel *model = [[GWBannersModel alloc] init];
            [model setValuesForKeysWithDictionary:modelDic];
            [self.bannersArr addObject:model];
        }
        [self addTableHeaderView];
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"网络请求失败");
    }];
    [operation1 start];
//
//    AFHTTPRequestOperation *operation2 = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://api.liwushuo.com/v2/promotions?gender=1&generation=1"]]];
//    [operation2 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
//        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves|NSJSONReadingMutableContainers error:nil];
//        for (NSDictionary *modelDic in dict[@"data"][@"promotions"]) {
//            GWPromotionsModel *model = [[GWPromotionsModel alloc] init];
//            [model setValuesForKeysWithDictionary:modelDic];
//            [self.promotionsArr addObject:model];
//        }
//        [self.tableView reloadData];
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        NSLog(@"网络请求失败");
//    }];
//    [operation2 start];
}

- (void)addTableHeaderView {
    
    self.tableView.tableHeaderView = ({
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 160)];
//        _arr = [[NSMutableArray alloc] initWithCapacity:0];
//        for (GWBannersModel *model in self.bannersArr) {
//            NSLog(@"%@", model.image_url);
//            [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString: model.image_url] options:SDWebImageDownloaderLowPriority progress:^(NSInteger receivedSize, NSInteger expectedSize) {
//                NSLog(@"%ld,%ld", receivedSize, expectedSize);
//            } completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
//                [_arr addObject:image];
//                if (_arr.count == 7) {
//                    _cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(5, 5, SCREEN_WIDTH-10, 150) imagesGroup:_arr];
//                    [view addSubview:_cycleScrollView];
//                }
//            }];
//        }
        NSMutableArray *arr = [NSMutableArray arrayWithCapacity:0];
        for (GWBannersModel *modelDic in self.bannersArr) {
            [arr addObject:[NSURL URLWithString:modelDic.image_url]];
        }
        _cycleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(5, 5, SCREEN_WIDTH-10, 150) imageURLsGroup:arr];
        [view addSubview:_cycleScrollView];
        view;
    });
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
//    return self.dataSourceArarry.count;
    return self.dataSourceArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GWItemsCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    // Configure the cell...
    GWItemsModel *model = _dataSourceArray[indexPath.row];
    [cell setConfigureModel:model];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    GWItemsModel *model = _dataSourceArray[indexPath.row];
    GWRaidersViewController *raidersVC = [[GWRaidersViewController alloc] init];
    raidersVC.id_s = model.id_s;
    [self.naviC pushViewController:raidersVC animated:YES];
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
